import {request} from '../../request/index.js';
let app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title:"",
        classify:1,
        flag:true,
        videopath:null,
        second: "0",
        uploadUrl:"",
        upbtn:false,
        show_video_progress: 0,
        progress_video_per: 0,
        userid:"",
        // 视频类别选择器
        multiIndex: [0, 0],
        multiArray: [[], []],
    },
    bindMultiPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        // 获取视频id
        request({url:app.data.url+"getClassIdByName/"+this.data.multiArray[1][this.data.multiIndex[1]]}).then(res=>{
            this.setData({classify:res.data.message});
        })
        this.setData({
          multiIndex: e.detail.value
        })
      },
      bindMultiPickerColumnChange: function (e) {
        console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
        var data = {
          multiArray: this.data.multiArray,
          multiIndex: this.data.multiIndex
        };
        data.multiIndex[e.detail.column] = e.detail.value;
        if(e.detail.column==0){
            let id = parseInt(e.detail.value)+1;
            request({url:app.data.url+"secondMenu/"+id}).then(res=>{
                data.multiArray[1] = res.data.message;
                this.setData({multiArray:data.multiArray});
            })
            // data.multiIndex[1] = 0;
            // this.setData(data);
        }
      },
    uploadVideo(e){
        let that = this;
        wx.chooseVideo({
            success(res){
                if(res.duration>60){
                    wx.showToast({
                        title: "不能超过60秒",
                        icon:"error"
                    })
                }else{
                    that.setData({
                        flag:false,
                        upbtn:true,
                        videopath:res.tempFilePath,
                        second:res.duration
                    })
                }
            },
            fail(err){
                console.log(err);
            }
        })
    },
    uploadToServer(e) {
        let that = this;
        this.setData({upbtn:false});
        wx.showToast({
            title: "上传中...",
            icon: "loading",
            duration:1000000,
            mask:true
        })
        wx.uploadFile({
            url: this.data.uploadUrl,
            filePath: e.currentTarget.dataset.videopath,
            name: 'upvideo',
            formData: {userid:this.data.userid,title:this.data.title,classify:this.data.classify},
            success: function (resp) {
                console.log(resp);
                if(resp.statusCode==200){
                    that.setData({
                        flag:true,
                        videopath:null,
                        second: "未拍摄",
                        uploadUrl:"",
                        upbtn:false,
                        show_video_progress: 0,
                        progress_video_per: 0
                    })
                    wx.showToast({
                        title: "上传成功",
                        icon:"success"
                    })
                    wx.switchTab({
                        url:"/pages/user/user"
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: "请联系客服",
                    icon:"error"
                })
            }
        })
    },
    // 页面加载获取用户id
    setUserId(){
        wx.getStorage({
          key: 'userState',
          success:res=>{
            request({url:app.data.url+"users/getUser",data:{code:res.data}}).then(result=>{
                this.setData({userid:result.data.message[0].id});
            })
          },
          fail:err=>{
            wx.showToast({
                title: '您还未登录',
                icon:"none"
              })
          }
        })
        // 设置视频类别
        let initMenu = this.data.multiArray
        request({url:app.data.url+"firstMenu"}).then(res=>{
            initMenu[0] = res.data.message;
            this.setData({multiArray:initMenu});
        })
        request({url:app.data.url+"secondMenu/1"}).then(res=>{
            initMenu[1] = res.data.message;
            this.setData({multiArray:initMenu});
        })
    },
    // 页面加载调用相机
    useCinema(){
        let that = this;
        wx.chooseVideo({
            sourceType:['camera'],
            success(res){
                if(res.duration>60){
                    wx.showToast({
                        title: "不能超过60秒",
                        icon:"error"
                    })
                }else{
                    that.setData({
                        flag:false,
                        upbtn:true,
                        videopath:res.tempFilePath,
                        second:res.duration
                    })
                }
            },
            fail(err){
                console.log(err);
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setUserId();
        this.useCinema();
        this.setData({
            uploadUrl:app.data.url+"uploadVideo"
        })
    },
    // 设置title
    inputValue(e){
        this.setData({title:e.detail.value});
    },
    // 取消
    cancel(){
        this.setData({
            title:"",
            classify:1,
            flag:true,
            videopath:null,
            second: "0",
            uploadUrl:"",
            upbtn:false,
            show_video_progress: 0,
            progress_video_per: 0
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        app.setTheme(app.data.themeid)
        this.setUserId();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})